package com.atguigu.dao.impl;

import com.atguigu.dao.ContainerDao;
import com.atguigu.pojo.Container;

import java.util.List;


public class ContainerDaoImpl extends BaseDao implements ContainerDao {

    @Override
    public int addContainer(Container container) {
        System.out.println("ContainerDaoImpl程序在[" + Thread.currentThread().getName() + "]线程中");
        String sql = "insert into t_container(`id`,`serverId`,`serviceId`,`name`,`imageVersion`,`cpu`,`memory`,`ip`,`volume`,`portMap`,`use`,`endTime`) values(?,?,?,?,?,?,?,?,?,?,?,?);";
        return update(sql,container.getId(), container.getServerId(),container.getServiceId(),container.getName(), container.getImageVersion(),container.getCpu(),container.getMemory(),container.getIp(),container.getVolume(),container.getPortMap(),container.getUse(), container.getEndTime());
    }

    @Override
    public int deleteContainerById(String id) {
        String sql = "delete from t_container where id=?";
        return update(sql, id);
    }

    @Override
    public int updateContainer(Container container) {
        String sql = "update t_container set `serverId`=?,`serviceId`=?,`name`=?,`imageVersion`=?,`cpu`=?,`memory`=?,`ip`=?,`volume`=?,`portMap`=?,`use`=?,`endTime`=? where id=?";
        return update(sql, container.getServerId(),container.getServiceId(),container.getName(), container.getImageVersion(),container.getCpu(),container.getMemory(),container.getIp(),container.getVolume(),container.getPortMap(),container.getUse(), container.getEndTime(), container.getId());
    }

    @Override
    public Container queryContainerById(String id) {
        String sql = "select `id`,`serverId`,`serviceId`,`name`,`imageVersion`,`cpu`,`memory`,`ip`,`volume`,`portMap`,`use`,`endTime` from t_container where id=?";
        return queryForOne(Container.class, sql, id);
    }
    @Override
    public List<Container> queryContainersByName(String containerName) {
        String sql = "select `id`,`serverId`,`serviceId`,`name`,`imageVersion`,`cpu`,`memory`,`ip`,`volume`,`portMap`,`use`,`endTime` from t_container where locate(?,name)>0";
        return queryForList(Container.class, sql, containerName);
    }

    @Override
    public List<Container> queryContainersByServerId(String serverId) {// 查询全部
        String sql = "select `id`,`serverId`,`serviceId`,`name`,`imageVersion`,`cpu`,`memory`,`ip`,`volume`,`portMap`,`use`,`endTime` from t_container where serverId=?";
        return queryForList(Container.class, sql,serverId);
    }
    @Override
    public List<Container> queryContainersByServerIdAndContainerName(String serverId,String containerName){
        String sql = "select `id`,`serverId`,`serviceId`,`name`,`imageVersion`,`cpu`,`memory`,`ip`,`volume`,`portMap`,`use`,`endTime` from t_container where serverId=? and locate(?,name)>0";
        return queryForList(Container.class, sql,serverId,containerName);
    }
    @Override
    public List<Container> queryContainers() {// 查询全部
        String sql = "select `id`,`serverId`,`serviceId`,`name`,`imageVersion`,`cpu`,`memory`,`ip`,`volume`,`portMap`,`use`,`endTime` from t_container";
        return queryForList(Container.class, sql);
    }

    @Override
    public Integer queryForPageTotalCount() {//查询总条目数,为page分片做准备
        String sql = "select count(*) from t_container";
        Number containerCount = (Number) queryForSingleValue(sql);//这个语句似乎可以返回任何类型的查询
        //System.out.println(bookCount);
        return containerCount.intValue();
    }

    @Override
    public List<Container> queryForPageItems(Integer begin, Integer pageSize) {
        String sql = "select `id`,`serverId`,`serviceId`,`name`,`imageVersion`,`cpu`,`memory`,`ip`,`volume`,`portMap`,`use`,`endTime` from t_container limit ?,?";
        return queryForList(Container.class, sql, begin, pageSize);//查询数据片段
    }
}


